package com.drojian.demo4.communication;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.drojian.demo.R;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class EventBusActivity extends AppCompatActivity {
    private Button btn_eventBus_send,btn_eventBus_sticky;
    private TextView tv1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.part4_activity_event_bus);
        initView();
        //1.注册及反注册
        EventBus.getDefault().register(EventBusActivity.this);
        initListener();
    }
        @Subscribe(threadMode = ThreadMode.MAIN)
        public void getMessage(EventMessage message){
            tv1.setText(message.toString());
        }
    private void initListener() {
        btn_eventBus_send.setOnClickListener(v->{
            startActivity(new Intent(EventBusActivity.this, EventBusReceiveActivity.class));
        });
        btn_eventBus_sticky.setOnClickListener(v->{
            EventBus.getDefault().postSticky(new EventStickyMessage("李四",20,"男"));
            startActivity(new Intent(EventBusActivity.this, EventBusReceiveActivity.class));

        });
    }

    private void initView() {
        btn_eventBus_send = findViewById(R.id.btn_eventBus_send);
        btn_eventBus_sticky = findViewById(R.id.btn_eventBus_sticky);
        tv1 = findViewById(R.id.tv1);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(EventBusActivity.this);
    }
}